package com.pj.wisdomlogistics.utils;

import android.content.Context;
import android.database.Cursor;

import com.pj.wisdomlogistics.entity.DBLoginInfoObj;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WuFan on 2017/9/28.
 */

public class LoginInfoHandler {
    private static LoginInfoHandler mHandler;
    private Context context;
    private static final String TBL_LOGIN_INFO = "LoginInfo";// 用户登陆信息
    private final String PREFS_NAME = "com.example.logintestdome";

    /**
     * 私有构造函数
     */
    private LoginInfoHandler(Context context) {
        this.context = context;
    }

    /**
     * 单例模式——保证对象唯一性
     *
     * @return
     */
    public static LoginInfoHandler create(Context context) {
        if (mHandler == null) {
            mHandler = new LoginInfoHandler(context);
        }
        return mHandler;
    }

    /**
     * 获取用户登陆信息
     *
     * @return
     */
    public List<DBLoginInfoObj> getLoginInfoObj() {
        List<DBLoginInfoObj> infoObjs = new ArrayList<DBLoginInfoObj>();
        DBHelper dbHelper = new DBHelper(context);
        Cursor c = null;
        try {
            c = dbHelper.quertAll(TBL_LOGIN_INFO);
            for (c.moveToLast(); !c.isBeforeFirst(); c.moveToPrevious()) {
                DBLoginInfoObj infoObj = new DBLoginInfoObj();
                infoObj.setID(c.getInt(c.getColumnIndex("_id")));
                infoObj.setUsername(c.getString(c.getColumnIndex("UserName")));
                infoObj.setPassword(c.getString(c.getColumnIndex("PassWord")));
                infoObjs.add(infoObj);

            }
        } catch (Exception e) {
            // TODO: handle exception
            Log.i("数据库异常：", e.toString());
        } finally {
            // 关闭游标
            c.close();
        }
        return infoObjs;
    }
}